/*

	tools23d - tools for converting images to 3D formats.
    Copyright (C) 2014  Dave Gorst

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "Windows.h"
#include <stdio.h>
#include <time.h>
#include <sys/stat.h>

#include "tools23d.h"
#include "glob-all.h"


static WIN32_FIND_DATA found;

/* Conversion from FILETIME to time_t pinched from here :
   http://www.gamedev.net/topic/565693-converting-filetime-to-time_t-on-windows/ */



/**
Populate globule according to file pattern (Windows version).

@param  globule Index of globule to populate.
@param  filename_pattern    Filename pattern with which to do so.
@return NUmber of entries added.
*/

int T3D_PopulateGlob (unsigned int globule, char * filename_pattern)
{
	HANDLE findhandle;
	DWORD last_error;
	LPSTR error_string;
	TCHAR fullpath [MAX_PATH];
	TCHAR filename_with_path [MAX_PATH];
	TCHAR * file_within_path;
	
	FILETIME creation_time;
	FILETIME last_access_time;
	FILETIME last_write_time;

	ULARGE_INTEGER temptime;
	time_t std_creation_time;

	HANDLE found_file_handle = INVALID_HANDLE_VALUE;
	int result = 0;
	
	GetFullPathName (filename_pattern, MAX_PATH, &fullpath [0], &file_within_path);

	if (file_within_path != NULL) *file_within_path = '\0';

	if ((findhandle = FindFirstFile (filename_pattern, &found)) != INVALID_HANDLE_VALUE)
	{
		do
		{
			if (!(found.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
			{
				sprintf_s (filename_with_path, MAX_PATH, "%s\\%s", fullpath, found.cFileName);

				found_file_handle = CreateFile(filename_with_path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
				if (found_file_handle == INVALID_HANDLE_VALUE) 
					break;
				GetFileTime (found_file_handle, &creation_time, &last_access_time, &last_write_time);
				temptime.HighPart = creation_time.dwHighDateTime;
				temptime.LowPart = creation_time.dwLowDateTime;
				std_creation_time = temptime.QuadPart / 10000000ULL - 11644473600ULL;
				T3D_AppendEntry (globule, filename_with_path, std_creation_time);
				CloseHandle (found_file_handle);
			}
			result = FindNextFile (findhandle, &found);

		}
		while (result);

		(void) FindClose (findhandle);
	}

	if ((findhandle == INVALID_HANDLE_VALUE) || (found_file_handle == INVALID_HANDLE_VALUE) || (!result))
	{
		last_error = GetLastError ();

		if (last_error != ERROR_NO_MORE_FILES)
		{
			FormatMessage
			(
				FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS,
				NULL,
				last_error,
				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
				(LPTSTR) &error_string,
				0,
				NULL
			);

			OutputDebugString (error_string);

			T3D_SetLastError (error_string);

			LocalFree (error_string);
		}
	}

	return (last_error == ERROR_NO_MORE_FILES) ? 0 : -1;
}
